package guru.springframework.controllers;

import com.google.gson.Gson;
import guru.springframework.domain.Modulebase;
import guru.springframework.services.ModuleBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by jt on 1/10/17.
 */
@Controller
public class ModuleBaseController {
    private ModuleBaseService moduleBaseService;

    @Value("${db.moduleUrl}")
    private String dbUrl;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    public void setModuleBaseService(ModuleBaseService moduleBaseService) {
        this.moduleBaseService = moduleBaseService;
    }



    @PostMapping("/moduleBaseList")
    @ResponseBody
    public String listModuleBases(HttpServletResponse response){
        List<Modulebase> modulebases=moduleBaseService.listAll();
        Gson gson = new Gson();
        return gson.toJson(modulebases);
    }

    @GetMapping("/getModuleBaseById/{id}")
    @ResponseBody
    public String getModuleBase(@PathVariable String id, HttpServletResponse response){
        Modulebase modulebase=moduleBaseService.getById(id);
        Gson gson = new Gson();
        return gson.toJson(modulebase);
    }

    @GetMapping("/getModuleBasesByPage")
    @ResponseBody
    public String listModuleBasesP(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        List<Modulebase> modulebases=moduleBaseService.getModulebases(pageNum,pageSize);
        Gson gson = new Gson();
//        Jdbi Database = Jdbi.create("jdbc:sqlite:javalinstagram.db");

        return gson.toJson(modulebases);
    }

    @PostMapping("/moduleBaseTosqlite")
    @ResponseBody
    public String moduleBaseTosqlite(HttpServletResponse response){
        moduleBaseService.saveDataToSqllie();
        Gson gson = new Gson();
        return gson.toJson("OK");
    }
}
